let BlackListModel = require('../model').blackListModel;
let DaoTool = require('../lib/daoTool');

exports.createBlackList = function (conn, params) {
    return new Promise(function (resolve, reject) {
        let sql = "insert into black_list set ?";
        conn.query(sql, [DaoTool.generateModelObject(params, BlackListModel, 'create')], function (err, result) {
            printLog("=createBlackList=", this.sql);
            if (err) reject(err);
            resolve(result)
        });
    });
};
exports.isBlack = function (conn,params) {
    return new Promise(function (resolve,reject) {
        conn.query('select * from black_list where openid=? and BlackOpenId=?', [params.openid,params.BlackOpenId], function (err, result) {
            printLog("=isBlack=", this.sql);
            if (err) reject(err);
            resolve(result)
        });
    });
};
exports.queryBlack = function (conn,params) {
    return new Promise(function (resolve,reject) {
        conn.query('select * from black_list where BlackId=?', [params.BlackId], function (err, result) {
            printLog("=queryBlack=", this.sql);
            if (err) reject(err);
            resolve(result)
        });
    });
};
exports.deleteBlack = function (conn,params) {
    return new Promise(function (resolve,reject) {
        conn.query('delete from black_list where BlackId=?;', [params.BlackId], function (err, result) {
            printLog("=deleteBlack=", this.sql);
            if (err) reject(err);
            resolve(result)
        });
    });
};
//获取好友列表
exports.selectBlack = function (conn,params,isCount) {
    return new Promise(function (resolve, reject) {
        let sql = " from black_list AS blt left join user as u ON u.openid = blt.BlackOpenId left join black_list bl on bl.BlackOpenId=? and bl.openid=blt.BlackOpenId left join my_option as myo on myo.openid=blt.BlackOpenId left join ta_option as tao on tao.openid = blt.BlackOpenId where blt.openid= ? "+ getSelectUserWhere(params.params);
        let selectSql = "select u.*,blt.BlackId,blt.IsFriend,bl.BlackId as IsBlack,myo.MyField147 as Birthday,myo.MyField148 as Height," + formatMatchingSqlStr(params.selfInfo) + sql + " order by ToMe desc" +DaoTool.getPageParamsSqlStr(params.params)+";";
        let countSql = "select count(1) as num" + sql;
        conn.query(selectSql, [params.params.openid,params.params.openid], function (err, selectResult) {
            printLog("=selectBlack err=",err);
            printLog("=selectBlack=",this.sql);
            if (err) reject(err);
            if (!isCount) resolve(selectResult);
            conn.query(countSql,[params.params.openid,params.params.openid],function (err,countResult) {
                printLog("=countSelectBlack err=",err);
                printLog("=countSelectBlack=",this.sql);
                if (err) reject(err);
                resolve({data:selectResult,count:countResult[0].num});
            });
        });
    });
};
function getSelectUserWhere(params) {
    let sql = "";
    sql+= params.UserNo ? " and u.UserNo = " + mysqlHelper.escape(params.UserNo) : "";
    sql+= params.City ? " and u.City = " + params.City : "";
    return sql;
}

function formatMatchingSqlStr(params) {
    try {
        let myo_sql = "";
        let tao_sql = "";
        for (let k = 0; k <= 68; k++) {
            let i = config.questionArr[k];
            if (!params["MyField" + i] || !params["TaField" + i])
                continue;
            if (myo_sql) {
                myo_sql += "+";
                tao_sql += "+";
            }
            myo_sql += '(case when myo.MyField' + i + ' is null then 0 else (matching(myo.MyField' + i + ',"' + params["TaField" + i] + '")) end)';
            tao_sql += '(case when tao.TaField' + i + ' is null then 0 else (matching("' + params["MyField" + i] + '",tao.TaField' + i + ')) end)';
        }
        myo_sql = "((" +(myo_sql?myo_sql:0)  + ")/68) as ToMe";
        tao_sql = "((" + (tao_sql?tao_sql:0) + ")/68) as ToTa ";
        return myo_sql + "," + tao_sql;
    } catch (err) {
        console.log(err);
    }
}